import { reactive } from 'vue'

export const usePagination = (initialPage = 1, initialPageSize = 10) => {
  const pagination = reactive({
    currentPage: initialPage,
    pageSize: initialPageSize,
    total: 0
  })

  // 分页数据计算
  const paginateData = (data) => {    
    pagination.total = data.length
    const start = (pagination.currentPage - 1) * pagination.pageSize
    const end = start + pagination.pageSize
    return data.slice(start, end)
  }

  // 处理页码变化
  const handlePageChange = (page) => {
    console.log(page)
    pagination.currentPage = page
  }

  // 处理每页条数变化
  const handleSizeChange = (size) => {
    pagination.pageSize = size
    pagination.currentPage = 1
  }

  // 重置分页
  const resetPagination = () => {
    pagination.currentPage = 1
  }

  return {
    pagination,
    paginateData,
    handlePageChange,
    handleSizeChange,
    resetPagination
  }
}